import Cookie from 'js-cookie'
export default {
  state: {
    currentMenu: null,
    isCollapse: false,
    menu: [],
    tableList: [
      {
        path:'/',
        name:'home',
        label:'首页',
        icon:'s-home'
      },
    ]
  },
  mutations: {
    //设置菜单的方法
    setMenu(state, val) {
      state.menu = val
      Cookie.set('menu',JSON.stringify(val))
      console.log(val,'vuex');
    },
    //退出登录后，清除菜单的方法
    clearMenu(state) {
      state.menu = []
      Cookie.remove('menu')
    },
    addMenu(state,router) {
      if (!Cookie.get('menu')) {
        return
      }
      let menu = JSON.parse(Cookie.get('menu'))
      state.menu = menu
      let currentMenu = [
        {
          path: '/',
          component: () => import(`@/views/Main`),
          children: []
        }
      ]
      menu.forEach(item => {
        if (item.children) {
          item.children = item.children.map(item => {
            item.component = () => import (`@/views/${item.url}`)
            return item
          })
          currentMenu[0].children.push(...item.children)
        } else {
          item.component = () => import (`@/views/${item.url}`)
          currentMenu[0].children.push(item)
        }
      })
      console.log(currentMenu, 'cur');
      router.addRoutes(currentMenu)
    },
    selectMenu(state, val) {
      if (val.name !== 'home') {
        state.currentMenu = val
        let result = state.tableList.findIndex(item=> item.name ===val.name)
        result === -1 ? state.tableList.push(val) : ''
      } else {
        state.currentMenu = null
      }
      /* val.name === 'home' ? state.currentMenu = null : state.currentMenu = val */
    },
    closeTab(state,val) {
      let result = state.tableList.findIndex(item => item.name === val.name)
        state.tableList.splice(result,1)
    },
    CollapseMenu(state) {
      return state.isCollapse = !state.isCollapse
    }
  },
  action: {}
}